package com.ubao.network;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

public class HttpConnection {
	private HttpURLConnection connEx;
	public static final String HEAD = "HEAD";
	public static final String GET = "GET";
	public static final String POST = "POST";
	private int resCode = Integer.MIN_VALUE;
	public static final int HTTP_OK = 200;

	public HttpConnection() {
	};
	
	
	
	public HttpURLConnection getConnEx() {
		return connEx;
	}



	public String getRequestProperty(String field) {
		return connEx.getRequestProperty(field);
	}

	public void setRequestProperty(String field, String newValue) throws java.io.IOException {
		connEx.setRequestProperty(field, newValue);
	}

	public void setRequestMethod(String arg) throws java.io.IOException {
		connEx.setRequestMethod(arg);
	}

	public void setConnEx(HttpURLConnection aConn) {
		this.connEx = aConn;
	}
	

	public int getResponseCode() throws java.io.IOException {
		if (resCode == Integer.MIN_VALUE) {
			resCode = connEx.getResponseCode();
			return connEx.getResponseCode();
		} else {
			return resCode;
		}
	}

	public String getHeaderField(String key) {
		// return connEx.getHeaderField(key);
		int temp = connEx.getHeaderFieldInt(key, 1024);
		return String.valueOf(temp);
	}

	public InputStream openInputStream() throws java.io.IOException {
		return connEx.getInputStream();
	}

	public OutputStream openOutputStream() throws java.io.IOException {
		return connEx.getOutputStream();
	}
	
	public InputStream openErrorInputStream() throws java.io.IOException {
		return connEx.getErrorStream();
	}

	public long getLength() {
		return connEx.getContentLength();
	}

	public String getRequestMethod() {
		return connEx.getRequestMethod();
	}

	public void close() throws java.io.IOException {
		connEx.disconnect();
	}
	
	public HttpURLConnection getHttpURLConnection() {
		return connEx;
	}
}
